const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    skin: '',
    typeId: 0,
    haveNext: false,
    page: 1,
    dataList: []
  },
  toPageZone(event) {
    console.log(event.currentTarget.dataset);
    var that = this;
    var oxtId = event.currentTarget.dataset.oxtId || 0;
    var typeId = that.data.typeId;
    wx.navigateTo({
      url: '/pages/zone/index?oxtId=' + oxtId + "&typeId=" + typeId
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //如果要在页面中使用
    this.setData({
      typeId: options.typeId || 0,
      page : 1
    });
    this.loadList(1);

    app.setNavBar()
    const { skin } = app.globalData
    this.setData({
      skin
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.haveNext) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadList(this.data.page);
    }
  },
  loadList: function (page) {
    let that = this;
    let getOxtallListParams = {
      "typeId": that.data.typeId,
      "page": page,
      "pageSize": 8,
    };
    utils.promiseGet('/wx/device/spray/getOxtallList', getOxtallListParams).then(res => {
      console.log(res);
      let oxtallList = that.data.dataList;
      let haveNext = that.data.haveNext;
      if (page == 1) {
        oxtallList = [];
      }
      if (res.code == 200) {
        let result = res.data;
        if (result.rows.length > 0) {
          for (let i = 0; i < result.rows.length; i++) {
            let itemRow = {
              "name" : result.rows[i].name,
              "normalSum" : result.rows[i].online,
              "errorSum" : result.rows[i].offline,
              "oxtId" : result.rows[i].oxtId,
              // 模拟数据
              obj: {
                'O2': result.rows[i].otwo,
                'H2S': result.rows[i].htwos,
                'Nh3': result.rows[i].nhthree,
                'Co2':result.rows[i].cotwo,
                'ETO': result.rows[i].eto,
                'THI': result.rows[i].thi,
                'temp': result.rows[i].temperature + '℃',
                'shidu': result.rows[i].humidity + '°'
              }
            };
            oxtallList.push(itemRow);
          }
          haveNext = true;
        } else {
          haveNext = false;
        }
        console.log(oxtallList);
      } else {
      }
      this.setData({
        dataList: oxtallList,
        haveNext: haveNext
      });
    }).catch(e => {
      console.log(e);
    });
  }
})